package com.lm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lm.pojo.*;
import com.lm.service.ResidentService;
import com.lm.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author LM
 * @version 1.0
 * @description: TODO
 * @date 2025/9/20 20:36
 */

@CrossOrigin
@Api(tags = "居民管理")
@RestController
@RequestMapping("/resident")
public class ResidentController {

    @Autowired
    private ResidentService residentService;

    /** 
     * @description:  
     * @param: residentId 
     * @return: com.lm.util.ResultUtil<java.util.List<com.lm.pojo.ResidentChatList>> 
     * @author 32477
     * @date: 2025/9/22 23:49
     */ 
    @ApiOperation(value = "查询居民", httpMethod = "GET", notes = "查询居民")
    @GetMapping("/findResident")
    public ResultUtil<List<ResidentChatList>> findResident(Integer residentId){
        try {
            return new ResultUtil<>(200, "查询成功", residentService.findResident(residentId));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "查询居民列表", httpMethod = "POST" ,notes = "查询所有居民信息")
    @PostMapping("/findResidentList")
    public ResultUtil<IPage<Resident>> findResidentList(@RequestBody SearchResident searchResident){
        try {
            IPage<Resident> pageInfo = residentService.findResidentList(searchResident);
            return new ResultUtil<>(200, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "添加居民部分信息", httpMethod = "POST", notes = "添加居民部分信息")
    @PostMapping("/addHalfResident")
    public ResultUtil addHalfResident(@RequestBody Resident resident){
        try {
            residentService.addHalfResident(resident);
            return new ResultUtil<>(200, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "添加失败");
        }
    }

    @ApiOperation(value = "添加居民全部信息", httpMethod = "POST", notes = "添加居民全部信息")
    @PostMapping("/addResident")
    public ResultUtil addResident(@RequestBody ResidentAndHealth  residentAndHealth){
        try {
            residentService.addResident(residentAndHealth);
            return new ResultUtil<>(200, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "添加失败");
        }
    }

    @ApiOperation(value = "查询居民姓名", httpMethod = "GET", notes = "查询居民姓名")
    @GetMapping("/findResidentName")
    public ResultUtil<List<Resident>> findResidentName(String name){
        try {
            return new ResultUtil<>(200, "查询成功", residentService.findResidentName(name));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }


    @ApiOperation(value = "查询居民标签", httpMethod = "GET", notes = "查询居民标签")
    @GetMapping("/findTags")
    public ResultUtil<List<Tag>> findTags(){
        try {
            return new ResultUtil<>(200, "查询成功", residentService.findTags());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "改变用户状态", httpMethod = "GET", notes = "改变用户状态")
    @GetMapping("/changeStatus")
    public ResultUtil changeStatus(Integer residentId,Integer status){
        try {
            residentService.changeStatus(residentId,status);
            return new ResultUtil<>(200, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "修改失败");
        }
    }


    @ApiOperation(value = "根据id查询居民信息", httpMethod = "GET" ,notes = "根据id查询居民信息")
    @GetMapping("/findResidentById")
    public ResultUtil<ResidentAndHealth> findResidentById(Integer residentId){
        try {
            return new ResultUtil<>(200, "查询成功", residentService.findResidentById(residentId));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "查询失败");
        }
    }

    @ApiOperation(value = "保存修改信息", httpMethod = "POST" ,notes = "保存修改信息")
    @PostMapping("/saveChange")
    public ResultUtil saveChange(@RequestBody ResidentAndHealth residentAndHealth){
        try {
            residentService.saveChange(residentAndHealth);
            return new ResultUtil<>(200, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "保存失败");
        }
    }

    @ApiOperation(value = "根据id删除用户",httpMethod = "DELETE", notes = "根据id删除用户")
    @DeleteMapping("/deleteResident")
    public ResultUtil deleteResident(Integer residentId){
        try {
            residentService.deleteResident(residentId);
            return new ResultUtil<>(200, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "删除失败");
        }
    }

}
